package com.lancoo.reviewsystem.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by tangminglong on 17/8/2.
 */

public class TimeUtils {

    /**
     * 服务器返回的时间格式
     */
    public static  final String SERVER_DATE_FORM ="yyyy-MM-dd'T'HH:mm:ss.SSS";
    public static  final String SERVER_DATE_FORM1 ="yyyy-MM-dd'T'HH:mm:ss.SSS";
    public static  final String SERVER_DATE_FORM2 ="yyyy-MM-dd'T'HH:mm:ss";
    private static final String DATE_FORM_LOCAL1 = "yyyy.MM.dd HH:mm";
    private static final String DATE_FORM_LOCAL3 = "yyyy.MM.dd";

    private static final String DATE_FORM_LOCAL2 = "yyyy.MM.dd";

    public static String getDate(Date date){
        return getDate(date, DATE_FORM_LOCAL1);

    }

    public static String getDateType2(Date date){
        return getDate(date, DATE_FORM_LOCAL3);

    }

    public static String getDate(Date date ,String dateForm){
        DateFormat dateFormat = new SimpleDateFormat(dateForm, Locale.CHINA);
        return dateFormat.format(date);
    }

    /**
     * 判断传入的时间是否是今天
     * @param dateFrom
     * @return
     */

    public static boolean dateIsToday(Date dateFrom){
       /* DateFormat dateFormat = new SimpleDateFormat(DATE_FORM_LOCAL2, Locale.CHINA);
        String currentDate = dateFormat.format(new Date());
        String reviewDate = dateFormat.format(date);
        return reviewDate.equals(currentDate);*/

        Calendar c1 = Calendar.getInstance();
        c1.setTime(dateFrom);
        int year1 = c1.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(new Date());
        int year2 = c2.get(Calendar.YEAR);
        int month2 = c2.get(Calendar.MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);

        return (year1 == year2 && month1 == month2 && day1 == day2);
    }

    /**
     * 判断传入的时间是否是昨天
     * *@param date
     * @return
     */
    public static boolean dateIsYestoday(Date dateFrom){
       /* Date currentDate = new Date();
        long currentDayInMilliSecond = currentDate.getTime()%(1000 * 60 * 24);
        long fromDayMilliSecond = dateFrom.getTime() % (1000 * 60 * 24);
        return currentDayInMilliSecond == fromDayMilliSecond + (1000 * 60 * 24);*/
       // DateFormat dateFormat = new SimpleDateFormat(DATE_FORM_LOCAL2, Locale.CHINA);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(dateFrom);
        int year1 = c1.get(Calendar.YEAR);
        int month1 = c1.get(Calendar.MONTH);
        int day1 = c1.get(Calendar.DAY_OF_MONTH);

        Calendar c2 = Calendar.getInstance();
        c2.setTime(new Date());
        int year2 = c2.get(Calendar.YEAR);
        int month2 = c2.get(Calendar.MONTH);
        int day2 = c2.get(Calendar.DAY_OF_MONTH);

       return (year1 == year2 && month1 == month2 && day1 +1 == day2);

    }

    /**
     * 传入秒获取时分秒
     * @param duration
     */
    public static String[] getHHmmssFromSeconds(int duration) {
        String[] result = new String[3];
        String ss;
        String mm;
        String hh;
        int s = duration % 60;
        int m = ((duration - s) % 3600)/60;
        int h = (duration - s - m*60)/3600;
        if (s < 10){
            ss = "0" + s;
        }else {
            ss = String.valueOf(s);
        }

        if (m < 10){
            mm = "0" + m;
        }else {
            mm = String.valueOf(m);
        }

        if (h < 10){
            hh = "0" + h;
        }else {
            hh = String.valueOf(h);
        }

        result[0] = hh;
        result[1] = mm;
        result[2] = ss;
        return result;
    }



    public static String getTimeType1FromMillis(int millis){
        SimpleDateFormat format = new SimpleDateFormat("mm:ss",Locale.CHINA);
        Date date = new Date(millis);
        return format.format(date);
    }
}
